<?php


namespace backend\modules\tool\DataSource\Log;
use backend\modules\tool\helpers\FileHelper;
use backend\modules\tool\models\SqlConfig;

define("back_database",dirname(__DIR__)."/Filesystem/back/");

class DataBaseBackHelper
{
    static $obj=[];
    protected $path;

    /**
     * @param $id
     * @return DataBaseBackHelper
     */
    public static function SingleTon($id){
        if(empty(self::$obj[$id])){
            self::$obj[$id]=new self($id);
        }
        return self::$obj[$id];
    }
    public function __construct($id)
    {
        $this->path=back_database."/".$id.'/';
        FileHelper::mkdir($this->path);
    }

    public function computeShell($config){
        $time=date("Y-m-d H_i_s");
        $name=$config->host."_".$config->data_base."_".$time;
        $name=str_replace(".","_",$name);
        $name=str_replace(" ","_",$name);
        $path=$this->path.$name.".sql";
        $path=str_replace('//',"/",$path);
        $shell="mysqldump -h{$config->host} -P{$config->port} -u{$config->user} -p{$config->password}  {$config->data_base}>{$path}";
        return ['shell'=>$shell,"path"=>$path];
    }
}